import Vue from "vue";

// 获取回答数
Vue.prototype.$getAnswerList = function (row) {
  if (!row) {
    return "";
  }
  if (row.answerContent3) {
    return 3;
  }
  if (row.answerContent2) {
    return 2;
  }
  if (row.answerContent1) {
    return 1;
  }
};

// 多图情况下获取logo
Vue.prototype.$getLogo = function (str) {
  if (!str || str.length <= 0) {
    return "";
  }
  if (Array.isArray(str)) {
    return str[0]
  }
  let arr = str.split(",");
  let _path = arr[0];
  return _path;
};

// 格式化参数stingify
Vue.prototype.$stringify = function (obj) {
  let str = "";
  for (let k in obj) {
    let o = "";
    if (obj[k]) {
      o = `&${k}=${obj[k]}`;
      str += o;
    }
  }
  str = str.slice(1);
  return str;
};

// 格式化换行
Vue.prototype.$formatTextarea = function (str) {
  if (!str) {
    return "";
  }
  let m = str.replace(/\n/g, "<br/>");
  return m;
};

// 路由跳转
Vue.prototype.$routerReplace = function (params) {
  //window.history.replaceState(null, '', '')
  let query = $nuxt.$route.query || {};
  let options = { ...query };
  if (query == options) {
    return false;
  }
  if (params) {
    options = Object.assign(query, params);
  }
  console.log($nuxt.$route.path)
  let str = $nuxt.$stringify(options)
  let _path = `${$nuxt.$route.path}?${str}`
  console.log(_path)
  $nuxt.$router.replace({
    path: _path
  });
};

Vue.prototype.$routerPush = function (path, params) {
  let query = $nuxt.$route.query || {};
  let options = { ...query };
  if (params) {
    options = Object.assign(query, params);
  }
  $nuxt.$router.push({
    path: path,
    query: options,
  });
};

// 获取当前城市
Vue.prototype.$getCity = function () {
  let _name = '全国'
  let city = $nuxt.$cookies.get('city');
  if (city && city.length) {
    _name = city
  }
  return _name;
}

// 格式化分数
Vue.prototype.$fomartRate = function (str) {
  let num = Number(str)
  if (!num) {
    return '0.0'
  }
  if (num > 5) {
    return '5.0';
  }
  let _rate = Number(str).toFixed(1);
  return _rate;
}